﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using emulib;

namespace TestServ
{
    /// <summary>
    /// Example server to document how to implement IMmoServ
    /// </summary>
    public class ExampleServ : IMmoServ
    {
        public String name = "Example Server for MMOEmu";
        public Int32 version = 1;
        public ServerInfo servInfo = new ServerInfo()
        {
            name = "Example Server for MMOEmu",
            version = 1,
            serverGuid = new Guid("{E9E007CF-7DA4-4452-9B3B-B4222F34CE0C}"),
            dbInfo = new DatabaseInfo()
            {
                tables = new Dictionary<String,List<DataColumn>>()
            }
        };
        //no idea what ^ (the variable declaration bits) is called but I like it
        String currentConfig;
        ConnectionManager cm;

        public ServerInfo GetInfo()
        {
            return servInfo;
        }
        public void LoadConfig(String config)
        {
            Console.WriteLine("Loading config from: " + config);
            currentConfig = config;
        }
        public void SaveConfig(String config)
        {
            Console.WriteLine("Saving config to: " + config);
            WorkDirectory wd = new WorkDirectory(this.servInfo);
            wd.WriteFile(config, "Test Data");
            currentConfig = config;
        }
        public void Run()
        {
            Console.WriteLine("Starting...");
            if (currentConfig == String.Empty)
            {
                Console.WriteLine("No configuration file given! Using config.txt");
                LoadConfig("config.txt");
            }
            cm = new ConnectionManager(new Dictionary<Byte[], Action>(), System.Net.IPAddress.Any, 1234);
            //System.Threading.Thread.Sleep(6000);
            //cm.StopListener();
        }
        public void Run(String config)
        {
            LoadConfig(config);
            Run();
        }
        public void Stop()
        {
            Console.WriteLine("Stopping...");
            cm.StopListener();
            if (currentConfig != null)
            {
                SaveConfig(currentConfig);
            }
        }
        public void Stop(String config)
        {
            SaveConfig(config);
            Stop();
        }
        public void HandleCmd(String[] cmd)
        {
            switch(cmd[0])
            {
                case "help":
                    Console.WriteLine("Help coming soon!");
                    break;
                case "something":
                    Console.WriteLine("The something command was given!");
                    break;
                case "load":
                    if (cmd.Length > 1)
                    {
                        LoadConfig(cmd[1]);
                    }
                    else
                    {
                        Console.WriteLine("No filename given, using config.txt instead.");
                        LoadConfig("config.txt");
                    }
                    break;
                case "save":
                    if (cmd.Length > 1)
                    {
                        SaveConfig(cmd[1]);
                    }
                    else
                    {
                        Console.WriteLine("No filename given, using config.txt instead.");
                        SaveConfig("config.txt");
                    }
                    break;
                case "dbsetup":
                    dbsetup();
                    break;
                default:
                    Console.WriteLine("Unknown command: " + String.Join(" ", cmd));
                    break;
            }
        }
        void dbsetup()
        {
            Console.WriteLine("Database setup");
            Console.WriteLine("Database type (SQLite, MSSQLCE, MSSQL, MySQL)");
            Console.Write(":");
            String dbtype = Console.ReadLine().ToUpper();
            if (dbtype.Equals("SQLITE") || dbtype.Equals("MSSQLCE"))
            {
                String dbExtenstion = dbtype == "SQLITE" ? ".db" : ".sdf";
                Console.WriteLine("Using serverDatabase" + dbExtenstion + " for database");
                Console.Write("Accept? (y/N)");
                ConsoleKeyInfo cki = Console.ReadKey(true);
                Console.WriteLine();
                if (cki.KeyChar.Equals('y') || cki.KeyChar.Equals('Y'))
                {
                    servInfo.dbInfo.dbTypeString = dbtype;
                    servInfo.dbInfo.sqldb = "serverDatabase" + dbExtenstion;
                    Console.WriteLine("Setup completed.");
                }
                else
                {
                    Console.WriteLine("Setup not completed. Configuration not changed.");
                }
            }
            else if (dbtype.Equals("MYSQL") || dbtype.Equals("MSSQL"))
            {
                Console.Write("Database host: ");
                String dbhost = Console.ReadLine();
                Console.Write("Database user: ");
                String dbuser = Console.ReadLine();
                Console.Write("Database pass: ");
                String dbpass = Extras.ReadPassword();
                Console.Write("Database name: ");
                String dbname = Console.ReadLine();
            }
            else
            {
                Console.WriteLine("Unknown database type");
            }
        }
    }
}
